package communication;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import java.util.List;

import sentData.*;

public class Communicator {

	public static String APP_ADDRESS = "http://vladimirrateapp.appspot.com/";

	private static URLConnection getServletConnection(String servletName)
			throws MalformedURLException, IOException {

		URL urlServlet = new URL(APP_ADDRESS + servletName);
		URLConnection con = urlServlet.openConnection();

		con.setDoInput(true);
		con.setDoOutput(true);
		con.setUseCaches(false);
		con.setRequestProperty("Content-Type",
				"application/x-java-serialized-object");
		return con;
	}

	public static void sendVote(String courseNumber, int date, String voter, int vote) {
		try {
			// send data to the servlet
			URLConnection con = getServletConnection("sendVote");
			OutputStream outstream = con.getOutputStream();
			ObjectOutputStream oos = new ObjectOutputStream(outstream);
			oos.writeObject(courseNumber);
			oos.writeObject(date);
			oos.writeObject(voter);
			oos.writeObject(vote);
			
			oos.flush();
			oos.close();

			readObject(con);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public static List<Course> getCourses() {
		URLConnection servletConnection;
		List<Course> result = new ArrayList<Course>();
		try {
			// System.out.println("get courses:");
			servletConnection = getServletConnection("download");

			outputEmptyObject(servletConnection);

			InputStream instr = null;
			ObjectInputStream inputFromServlet = null;
			try {
				instr = servletConnection.getInputStream();
				inputFromServlet = new ObjectInputStream(instr);
				Course readCourse;
				while ((readCourse = (Course) inputFromServlet.readObject()) != null) {
					result.add(readCourse);
					// System.out.println("received");
				}
			} finally {
				inputFromServlet.close();
				instr.close();
			}
		} catch (Exception e) {
			//e.printStackTrace();
		}
		return result;
	}

	private static void readObject(URLConnection con) throws IOException,
			ClassNotFoundException {
		InputStream instr = con.getInputStream();
		ObjectInputStream inputFromServlet = new ObjectInputStream(instr);
		inputFromServlet.readObject();
		inputFromServlet.close();
		instr.close();
	}

	private static void outputEmptyObject(URLConnection servletConnection)
			throws IOException {
		OutputStream outstream = servletConnection.getOutputStream();
		ObjectOutputStream oos = new ObjectOutputStream(outstream);
		oos.writeObject("_");
		oos.flush();
		oos.close();
	}

	public static void main(String args[]) throws MalformedURLException,
			IOException {
		List<Course> courses = getCourses();
		for (Course c : courses) {
			System.out.println(c);
		}
		System.out.println("*****");

		sendVote("III000", 20130218, "tester", 5);
		
		courses = getCourses();
		for (Course c : courses) {
			System.out.println(c);
		}

	}

}
